/**
 * @author 汪孟苹
 */
define(['jquery','bbcare','echarts','dm/vm/physicalExaminationAnalysisReportData'],function($,$bb,echarts,$temperature){
    var physicalExaminationAnalysisReport = {};
    var URL = $bb.baseUrl("department")+"followup/service";
    physicalExaminationAnalysisReport.init = function(){
        /* 体温列表 */
        var temObj=$bb.getPageParams();
        var userId =temObj.userId;
        $bb.ajax({
            url: URL + "/patient/temperature/history",
            type:"post",
            contentType: "application/json;charset=UTF-8",
            dataType:"json",
            data:JSON.stringify({
                data : {
                    "userId": userId
                }
            }),
            success: function(data){
                for(var i=0;i<data.rows.length;i++){
                    var timeLong=data.rows[i].timeLong;
                    data.rows[i].timeLong=$temperature.timeLong(timeLong);
                }
                var bodytemperatureId = data.rows[0].bodytemperatureId;
                init();
                $("#listBox .bottom-con").eq(0).addClass("active");
                $("#listBox .bottom-con").eq(0).find("i").addClass("fff");
                detail(bodytemperatureId);
                temperatureTab();
                function temperatureTab(){
                    $("#listBox .bottom-con").unbind("click");
                    $("#listBox .bottom-con").click(function(){
                        $(this).addClass("active").siblings(".bottom-con").removeClass("active");
                        $(this).find("i").addClass("fff");
                        $(this).siblings(".bottom-con").find("i").removeClass("fff");
                        var bodytemperatureId = $(this).attr("bodytemperatureId");
                        detail(bodytemperatureId);
                        temperatureTab();
                    });
                }
                function init(){
                    $temperature.listInit({
                        id:"listBox",
                        list:data.rows,
                        bodytemperatureId:bodytemperatureId
                    });
                }
                function detail(bodytemperatureId){
                    $bb.ajax({
                        url: URL + "/patient/temperature/detail",
                        type:"post",
                        contentType: "application/json;charset=UTF-8",
                        dataType:"json",
                        data:JSON.stringify({
                            data : {
                                "bodytemperatureId": bodytemperatureId
                            }
                        }),
                        success: function(data){
                            temObj.birthday=data.data.birthday;
                            temObj.username=nameLens(data.data.userName);
                            $temperature.userInfoInit({
                                id:"tem_userInfo",
                                list:temObj
                            });
                            $temperature.DetailInfoInit({
                                id:"detailInfo",
                                list:data.data
                            });
                            var tempalarmTop = data.data.tempalarmTop;
                            var perTop = data.data.perTop; 
                            var arrTemperature = $temperature.tempalarm(tempalarmTop,perTop);
                            arcircular1(perTop,arrTemperature);
                            var bodyTemperatureDetails = data.data.bodyTemperatureDetails;
                            var returnVal = $temperature.lineData(bodyTemperatureDetails);
                            var arrTime = returnVal.arrTime;
                            var arrTemp = returnVal.arrTemp;
                            trend(arrTime,arrTemp);
                            function arcircular1(perTop,arrTemperature){ //中间饼形图
                                var myChart = echarts.init(document.getElementById('arcircular1'));
                                option = {
                                    color:['#26C6DA','#FE7054'],
                                    tooltip: {
                                        trigger: 'item',
                                        formatter: "{b}"
                                    },
                                    legend: {
                                        orient: 'vertical',
                                        bottom:'20px',
                                        data:arrTemperature
                                    },
                                    series: [
                                        {
                                            name:'',
                                            type:'pie',
                                            radius: ['40%', '60%'],
                                            avoidLabelOverlap: false,
                                            label: {
                                                normal: {
                                                    show: false,
                                                    position: 'center',
                                                }
                                            },
                                            labelLine: {
                                                normal: {
                                                    show: false
                                                }
                                            },
                                            center: ['50%', '40%'],
                                            data:[
                                                {
                                                    value:100-perTop, 
                                                    name:arrTemperature[0],
                                                    label: {
                                                        normal: {
                                                            show: true,
                                                            position: 'center',
                                                            formatter:'体温 \n\n（℃）',
                                                            textStyle:{
                                                                fontSize: 14,
                                                                color:'#333'
                                                            }
                                                        }
                                                    }
                                                },
                                                {value:perTop, name:arrTemperature[1]},
                                            ]
                                        }
                                    ]
                                };
                                myChart.setOption(option);
                            }
                            function trend(arrTime,arrTemp){ //下方折线图
                                var myChart = echarts.init(document.getElementById('trend'));
                                option = {
                                    title: {
                                        text: '',
                                        padding:[10,10],
                                    },
                                    tooltip: {
                                        trigger: 'axis'
                                    },
                                    legend: {
                                        data:['体温']
                                    },
                                    grid: {
                                        left: '3%',
                                        right: '4%',
                                        bottom: '3%',
                                        containLabel: true
                                    },
                                    xAxis: {
                                        type: 'category',
                                        boundaryGap: false,
                                        data: arrTime
                                    },
                                    yAxis: {
                                        type: 'value'
                                    },
                                    series: [
                                        {
                                            name:'温度',
                                            type:'line',
                                            stack: '总量',
                                            symbol:'none',  //去掉点
                                            smooth:true,    //曲线变平滑 
                                            data: arrTemp
                                        },
                                    ]
                                };
                                myChart.setOption(option);
                            }
                        }
                    });
                }

            }
        });
        //左边内容模块第一个折线图
        $(function(){
            $(window).resize(function(){
                var width=  $("#trend").width();
                $("#trend >  div").css({"width":width});
                $("#trend >  div > canvas").css({"width":width});
            });
        });
        function nameLens(name){
            if(name.length>8){
                var username=name.substring(0,8);
            }else{
                username=name;
            }
            return username;
        }
    }
    return physicalExaminationAnalysisReport;
});